#include "lmanagerview_console_impl.h"
#include "managerview_impl.h"
#include "userview_impl.h"
#include "lmanagerview.h"
#include "tools.h"
#include "lmanagerservice_impl.h"
#include "lmanagerdao.h"
#include "lmanagerdao_file_impl.h"
#include "lmanager.h"
#include <iostream>
#include<iomanip>
#include <vector>
#include <string.h>
using namespace std;
using namespace MC;
vector<Lmanager*> lmanagers;

void hide_pwd(char *pwd)
{
	getch();
	char count;
	int i = 0;
	while(count!='\n' && pwd[i])
	{
		count = getch();
		if(count == 127)
		{
			if(i > 0)
			{
				i--;
				cout << "\b \b";
			}
		}
		else if(count == 10) 
		{
			pwd[i] = '\0';
			break;
		}
		else
		{
			cout << "*";
			pwd[i++] = count;
		}
	}
	getch();
	cout << endl;
}
int main()
{
	init_id();
    ManagerViewImpl* manager = new ManagerViewImpl;
	UserViewImpl* user = new UserViewImpl;
	LmanagerView* lmanager = new LmanagerviewConsoleImpl;
	LmanagerDao* dao = new LmanagerDaoFileImpl;
	while(1)
	{
	    system("clear");
	    cout << "*******欢迎来到图书管理系统*******" << endl;
	    cout << "\e[33m         1、root模式\e[0m" << endl;
	    cout << "\e[33m         2、管理员系统\e[0m" << endl;
	    cout << "\e[33m         3、用户系统\e[0m" << endl;
        cout << "\e[33m         4、退出\e[0m" << endl;
        cout << "**********************************" << endl;
		cout << "\e[34m请输入指令: \n\e[0m";
		char cmd = 0;
		while(1)
		{
			cmd = getch();
			if(cmd >='1' && cmd <='4')
				break;
		}
		cout << cmd << endl;
		system("clear");
		switch(cmd-'0')
		{
			case 1:
			    {
                    int root_id;
                    char root_passward[7] = "000000";
					cout << "\e[34m请输入超级管理员ID：\e[0m" << endl;
					cin >> root_id;
					cout << "\e[34m请输入超级管理员密码：\e[0m" << endl;
					hide_pwd(root_passward);
					if(root_id == 123456 && strcmp(root_passward,"123456")==0)
						{
							cout << "\e[36m登陆成功!!!\e[0m" <<endl;
							anykey_continue();
							lmanager->menu();break;
					    }
                }
			
		    case 2:
				{
					dao->load(lmanagers);
					int _id;
                    char _passward[7] = "000000";
					cout << "\e[34m请输入管理员ID：\e[0m" << endl;
					cin >> _id;
					cout << "\e[34m请输入管理员密码：\e[0m" << endl;
					hide_pwd(_passward);
					unsigned int i =0 ;
					for(; i<lmanagers.size();i++)
					{
		 				if(_id == lmanagers.at(i)->get_id() && strcmp(_passward,lmanagers.at(i)->get_password())==0)
						{
							cout << "\e[36m登陆成功!!!\e[0m" <<endl;
							anykey_continue();
							manager->Run();
							break;
						}
					}
					if(i>=lmanagers.size())
					{
					    cout << "\e[31m登陆失败，请重新选择！！！！\e[0m" << endl;
					    anykey_continue();break;
					}
				}    break;
			case 3:user->Menu(); break;
		    case 4:return 0;		
		}
	}
	delete lmanager;
	delete manager;
	delete user;
}
